<?php
  $clientKey = $_REQUEST['ClientKey'];
  $scenario  = json_decode($_REQUEST['Scenario'],TRUE);
  $oil       = json_decode($_REQUEST['OilInfo'],TRUE);
  $env       = json_decode($_REQUEST['Environments'],TRUE);
  $targetNs  = $_REQUEST['targetNs'];
  $targetUrl = $_REQUEST['loadSaveUrl'];

  $xml = array();
  array_push($xml,<<< EOXML
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <SOAP-ENV:Body>
        <tns:CreateScenario xmlns:tns="$targetNs">
EOXML
  ); 

  array_push($xml,"<tns:ClientKey>$clientKey</tns:ClientKey>");

  $inputXml = array();

  array_push($inputXml,"<Scenario>");

  foreach ($scenario as $k => $v) {
    array_push($inputXml,"<$k>$v</$k>");
  }

  array_push($inputXml,"<Environments>");
  for ($i = 0; $i < count($env); $i++) {
    array_push($inputXml,"<Environment>");
    foreach ($env[$i] as $k => $v) {
      array_push($inputXml,"<$k>$v</$k>");
    }
    array_push($inputXml,"</Environment>");
  }
  array_push($inputXml,"</Environments>");

  array_push($inputXml,"<OilInfo>");
  foreach ($oil as $k => $v) {
    array_push($inputXml,"<$k>$v</$k>");
  }
  array_push($inputXml,"</OilInfo>");

  array_push($inputXml,"</Scenario>");

  array_push($xml,"<tns:Inputxml>".implode("",str_replace('<','&lt;',str_replace('>','&gt;',$inputXml)))."</tns:Inputxml>");

  array_push($xml,<<< EOXML
        </tns:CreateScenario>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
EOXML
  );

  echo file_get_contents(
     $targetUrl
    ,false
    ,stream_context_create(
      array('http' => array(
         'method'  => 'POST'
        ,'header'  => 'Content-type: text/xml'
        ,'content' => implode("\n",$xml)
      ))
    )
  );
?>
